﻿// exercise_10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
// 7-1 考试座位号

struct Student
{
	char num[17];
	int test_seat;
	int exam_seat;
};

struct Student students[1000];

int find_seat(int test_seat, int n)
{
	for (int i = 0; i < n; i++)
	{
		if (test_seat == students[i].test_seat)
			return i;
	}
	return -1;
}

void ex10_7_1()
{
	int n, k;
	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf("%s%d%d", students[i].num, &students[i].test_seat, &students[i].exam_seat);
	}

	scanf("%d", &k);

	int test_seat = 0, index;

	for (int i = 0; i < k; i++)
	{
		scanf("%d", &test_seat);
		index = find_seat(test_seat, n);
		printf("%s %d\n", students[index].num, students[index].exam_seat);
	}
}

// 7-2 复数四则运算
typedef struct Complex
{
	float real, imag;
} Complex;

void input(Complex *c)
{
	scanf("%f%f", &c->real, &c->imag);
}

void output(Complex c)
{
	printf("(%.2f%+.2fi)\n", c.real, c.imag);
}

float magnitude(Complex c)
{
	return c.real * c.real + c.imag * c.imag;
}

Complex conjugate(Complex c)
{
	Complex cc = {c.real, -c.imag};
	return cc;
}

Complex scale(Complex c, float factor)
{
	Complex cc;
	cc.real = c.real / factor;
	cc.imag = c.imag / factor;
	return cc;
}

Complex add(Complex c1, Complex c2)
{
	Complex c;
	c.real = c1.real + c2.real;
	c.imag = c1.imag + c2.imag;
	return c;
}

Complex sub(Complex c1, Complex c2)
{
	Complex c;
	c.real = c1.real - c2.real;
	c.imag = c1.imag - c2.imag;
	return c;
}

Complex mul(Complex c1, Complex c2)
{
	Complex c;
	c.real = c1.real * c2.real - c1.imag * c2.imag;
	c.imag = c1.real * c2.imag + c1.imag * c2.real;
	return c;
}

Complex div(Complex c1, Complex c2)
{
	return scale(mul(c1, conjugate(c2)), magnitude(c2));
}

void print(Complex c1, Complex c2, Complex c3, char op)
{
	printf("(%.1f%+.1fi) %c (%.1f%+.1fi) = ", c1.real, c1.imag, op, c2.real, c2.imag);

	if (fabs(c3.real) < 0.05 && fabs(c3.imag) < 0.05)
	{
		printf("0.0\n");
		return;
	}

	if (fabs(c3.real) > 0.05 && fabs(c3.imag) > 0.05)
	{
		printf("%.1f%+.1fi\n", c3.real, c3.imag);
		return;
	}

	if (fabs(c3.real) > 0.05)
		printf("%.1f\n", c3.real);

	if (fabs(c3.imag) > 0.05)
		printf("%.1fi\n", c3.imag);
}

void ex10_7_2()
{
	Complex c1, c2;
	input(&c1);
	input(&c2);
	print(c1, c2, add(c1, c2), '+');
	print(c1, c2, sub(c1, c2), '-');
	print(c1, c2, mul(c1, c2), '*');
	print(c1, c2, div(c1, c2), '/');
}

// 7-3 通讯录的录入与显示
typedef struct
{
	char name[11];
	char date[11];
	char gender[2];
	char tel[18];
	char mobile[18];
} Contact;

void ex10_7_3()
{
	int N, K;
	Contact contacts[100];
	scanf("%d", &N);
	for (int i = 0; i < N; i++)
	{
		scanf("%s%s%s%s%s", contacts[i].name, contacts[i].date, contacts[i].gender, contacts[i].tel, contacts[i].mobile);
	}
	scanf("%d", &K);
	int index = 0;
	for (int i = 0; i < K; i++)
	{
		scanf("%d", &index);
		if (index >= N || index < 0)
			printf("Not Found\n");
		else
			printf("%s %s %s %s %s\n", contacts[index].name, contacts[index].tel, contacts[index].mobile, contacts[index].gender, contacts[index].date);
	}
}

// 7-4 通讯录排序

void swap(Contact *c1, Contact *c2)
{
	Contact t = *c1;
	*c1 = *c2;
	*c2 = t;
}

void sort(Contact contacts[], int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < n - i - 1; j++)
		{
			if (strcmp(contacts[j].date, contacts[j + 1].date) > 0)
				swap(contacts + j, contacts + j + 1);
		}
	}
}

int cmp(const void* a, const void* b)
{
    Contact c1 = *(Contact*)a;
    Contact c2 = *(Contact*)b;
    return strcmp(c1.date,c2.date);
}

void ex10_7_4()
{
	int n;
	Contact contacts[10];

	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf("%s%s%s", contacts[i].name, contacts[i].date, contacts[i].tel);
	}

	//sort(contacts, n);
	qsort(contacts, n, sizeof(contacts[0]), cmp);
	for (int i = 0; i < n; i++)
	{
		printf("%s %s %s\n", contacts[i].name, contacts[i].date, contacts[i].tel);
	}
}
int main()
{
	return 0;
}